import { addUserClassRelationUsingPost } from '@/services/backend/userClassRelationController';
import {ProColumns, ProSchemaValueEnumMap, ProTable} from '@ant-design/pro-components';
import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';

interface Props {
  modalVisible: boolean;
  classInfoEnumMap: ProSchemaValueEnumMap;
  userEnumMap: ProSchemaValueEnumMap;
  onSubmit: () => void;
  onCancel: () => void;
}

/**
 * @en-US Add node
 * @zh-CN 添加节点
 * @param fields
 */
const handleAdd = async (fields: API.UserClassRelationAddRequest) => {
  const hide = message.loading('正在添加');
  try {
    await addUserClassRelationUsingPost({
      ...fields,
    });
    hide();
    message.success('创建成功');
    return true;
  } catch (error: any) {
    hide();
    message.error(`创建失败,${error.message}`);
    return false;
  }
};

const CreateModal: React.FC<Props> = (props) => {
  const { modalVisible, classInfoEnumMap,userEnumMap, onSubmit, onCancel } = props;

  const columns: ProColumns<API.UserClassRelationVO>[] = [
    {
      title: '用户姓名',
      dataIndex: 'userId',
      valueEnum: userEnumMap,
      hideInSearch: true,
    },
    {
      title: '班级名称',
      dataIndex: 'classId',
      valueEnum: classInfoEnumMap,
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        active: { text: '开启', status: 'active' },
        archived: { text: '关闭', status: 'exit' },
      },
    },
    {
      title: '用户班级角色',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        active: { text: '学生', status: 'student' },
        archived: { text: '助手', status: 'assistant' },
      },
    },
  ];

  return (
    <Modal title={'新建'} open={modalVisible} destroyOnClose footer={null} onCancel={onCancel}>
      <ProTable
        type={'form'}
        columns={columns}
        onSubmit={async (value) => {
          const success = await handleAdd(value);
          if (success) {
            onSubmit?.();
          }
        }}
      />
    </Modal>
  );
};
export default CreateModal;
